function Room() {
	this.publicBlogs = [];
	this.map = null;
	this.marker = null;
	this.circle = null;
	this.rectangle = null;
	this.polyline = null;
	this.polygon = null;
	this.infowindow = new google.maps.InfoWindow();
	this.adapter = new ClientAdapter("/CoupledObjectWebServer/");
	this.adapter.joinSession("feed");
	this.adapter.start(500);
	this.personalAdapter = new ClientAdapter("/CoupledObjectWebServer/");
	this.personalAdapter.joinSession("sync");
	var userId = this.personalAdapter.userId;
	this.personalAdapter.leaveSession();
	this.personalAdapter.joinSession("intop-" + userId);
	this.personalAdapter.start(500);
	this.selected = null;
	this.couple();
}

Room.prototype.getNewId = function() {
	return Math.floor((Math.random() * 100000) + 1) + '_'
			+ this.adapter.clientId;
};

Room.prototype.deleteOverlay = function(overlay) {
	if (overlay == 'circle')
		this.circle.setVisible(false);
	else if (overlay == 'rectangle')
		this.rectangle.setVisible(false);
	else if (overlay == 'polyline')
		this.polyline.setVisible(false);
	else if (overlay == 'polygon')
		this.polygon.setVisible(false);
	else if (overlay == 'marker')
		this.marker.setVisible(false);
	drawingControl(true);
};

Room.prototype.addBlog = function(marker) {
	this.marker = marker;
	listenerAdd('marker');
	google.maps.event.addListener(marker, 'click', function() {
		listenerAdd('marker');
	});
};

Room.prototype.addBlogCircle = function(circle) {
	this.circle = circle;
	listenerAdd('circle');
	google.maps.event.addListener(circle, 'click', function() {
		listenerAdd('circle');
	});
};

Room.prototype.addBlogPolygon = function(polygon) {
	this.polygon = polygon;
	listenerAdd('polygon');
	google.maps.event.addListener(polygon, 'click', function() {
		listenerAdd('polygon');
	});
};

Room.prototype.addBlogPolyline = function(polyline) {
	this.polyline = polyline;
	listenerAdd('polyline');
	google.maps.event.addListener(polyline, 'click', function() {
		listenerAdd('polyline');
	});
};

Room.prototype.addBlogRectangle = function(rectangle) {
	this.rectangle = rectangle;
	listenerAdd('rectangle');
	google.maps.event.addListener(rectangle, 'click', function() {
		listenerAdd('rectangle');
	});
};

Room.prototype.publishBlog = function(files, tags) {
	this.marker.setVisible(false);
	drawingControl(true);
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publish(blogId, this.marker.getPosition().lat(), this.marker
			.getPosition().lng(), $("#title").val(), $("#description").val(),
			files, tags, time, $('#userName').val());
};

Room.prototype.publishBlogCircle = function(files, tags) {
	this.circle.setVisible(false);
	drawingControl(true);
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishCircle(blogId, this.circle.getCenter().lat(), this.circle
			.getCenter().lng(), this.circle.getRadius(), $("#title").val(), $(
			"#description").val(), files, tags, time, $('#userName').val());
};

Room.prototype.publishBlogRectangle = function(files, tags) {
	this.rectangle.setVisible(false);
	drawingControl(true);
	var sw = this.rectangle.getBounds().getSouthWest();
	var ne = this.rectangle.getBounds().getNorthEast();
	var boundArray = [ sw.lat(), sw.lng(), ne.lat(), ne.lng() ];
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishRectangle(blogId, boundArray, $("#title").val(), $(
			"#description").val(), files, tags, time, $('#userName').val());
};

Room.prototype.publishBlogPolyline = function(files, tags) {
	this.polyline.setVisible(false);
	drawingControl(true);
	var pathArray = this.polyline.getPath().getArray();
	var array = [];
	var length = this.polyline.getPath().getLength();
	for (var i = 0, j = 0; i < length; i++, j = j + 2) {
		array[j] = pathArray[i].lat();
		array[j + 1] = pathArray[i].lng();
	}
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishPolyline(blogId, array, length, $("#title").val(), $(
			"#description").val(), files, tags, time, $('#userName').val());
};

Room.prototype.publishBlogPolygon = function(files, tags) {
	this.polygon.setVisible(false);
	drawingControl(true);
	var pathArray = this.polygon.getPath().getArray();
	var array = [];
	var length = this.polygon.getPath().getLength();
	for (var i = 0, j = 0; i < length; i++, j = j + 2) {
		array[j] = pathArray[i].lat();
		array[j + 1] = pathArray[i].lng();
	}
	$("#buttonD").click();
	var blogId = this.getNewId();
	$("#blogId").val(blogId);
	var time = getTime();
	this.publishPolygon(blogId, array, length, $("#title").val(), $(
			"#description").val(), files, tags, time, $('#userName').val());
};

Room.prototype.publish = function(blogId, lat, lng, title, description, files,
		tags, time, userId) {
	var latlng = new google.maps.LatLng(lat, lng);
	var marker = new google.maps.Marker({
		position : latlng,
		draggable : false,
		map : this.map
	});
	var n = this.publicBlogs.length;
	this.publicBlogs[n] = new Blog(marker, blogId, title, description, time,
			'marker', userId);
	google.maps.event.addListener(marker, 'click', function() {
		listenerPublish(blogId, title, description, files, time);
		app.currentRoom.infowindow.close();
		app.currentRoom.infowindow.setContent(title);
		app.currentRoom.infowindow.open(app.currentRoom.map, marker);
		app.currentRoom.select(blogId);
	});
};

Room.prototype.publishCircle = function(blogId, lat, lng, radius, title,
		description, files, tags, time, userId) {
	var latlng = new google.maps.LatLng(lat, lng);
	var circleOptions = {
		strokeColor : "#B40404",
		strokeWeight : 2,
		fillColor : "#000000",
		fillOpacity : 0.2,
		map : this.map,
		center : latlng,
		radius : radius,
		clickable : true,
		editable : false,
		draggable : false,
		zIndex : 1
	};
	var circle = new google.maps.Circle(circleOptions);
	var n = this.publicBlogs.length;
	this.publicBlogs[n] = new Blog(circle, blogId, title, description, time,
			'circle', userId);
	google.maps.event.addListener(circle, 'click', function(ev) {
		listenerPublish(blogId, title, description, files, time);
		app.currentRoom.infowindow.close();
		app.currentRoom.infowindow.setPosition(ev.latLng);
		app.currentRoom.infowindow.setContent(title);
		app.currentRoom.infowindow.open(app.currentRoom.map);
		app.currentRoom.select(blogId);
	});
};

Room.prototype.publishRectangle = function(blogId, boundsArray, title,
		description, files, tags, time, userId) {
	var sw = new google.maps.LatLng(boundsArray[0], boundsArray[1]);
	var ne = new google.maps.LatLng(boundsArray[2], boundsArray[3]);
	var bnd = new google.maps.LatLngBounds(sw, ne);
	var rectangleOptions = {
		strokeColor : "#B40404",
		strokeWeight : 2,
		fillColor : "#000000",
		fillOpacity : 0.2,
		map : this.map,
		bounds : bnd,
		clickable : true,
		editable : false,
		draggable : false,
		zIndex : 1
	};
	var rectangle = new google.maps.Rectangle(rectangleOptions);
	var n = this.publicBlogs.length;
	this.publicBlogs[n] = new Blog(rectangle, blogId, title, description, time,
			'rectangle', userId);
	google.maps.event.addListener(rectangle, 'click', function(ev) {
		listenerPublish(blogId, title, description, files, time);
		app.currentRoom.infowindow.close();
		app.currentRoom.infowindow.setPosition(ev.latLng);
		app.currentRoom.infowindow.setContent(title);
		app.currentRoom.infowindow.open(app.currentRoom.map);
		app.currentRoom.select(blogId);
	});
};

Room.prototype.publishPolyline = function(blogId, pathArray, length, title,
		description, files, tags, time, userId) {
	var mvcArray = [];
	for (var i = 0, j = 0; i < length; i++, j = j + 2) {
		mvcArray[i] = new google.maps.LatLng(pathArray[j], pathArray[j + 1]);
	}
	var polylineOptions = {
		strokeColor : "#B40404",
		strokeWeight : 2,
		map : this.map,
		path : mvcArray,
		clickable : true,
		editable : false,
		draggable : false,
		zIndex : 1
	};
	var polyline = new google.maps.Polyline(polylineOptions);
	var n = this.publicBlogs.length;
	this.publicBlogs[n] = new Blog(polyline, blogId, title, description, time,
			'polyline', userId);
	google.maps.event.addListener(polyline, 'click', function(ev) {
		listenerPublish(blogId, title, description, files, time);
		app.currentRoom.infowindow.close();
		app.currentRoom.infowindow.setPosition(ev.latLng);
		app.currentRoom.infowindow.setContent(title);
		app.currentRoom.infowindow.open(app.currentRoom.map);
		app.currentRoom.select(blogId);
	});
};

Room.prototype.publishPolygon = function(blogId, pathArray, length, title,
		description, files, tags, time, userId) {
	var mvcArray = [];
	for (var i = 0, j = 0; i < length; i++, j = j + 2) {
		mvcArray[i] = new google.maps.LatLng(pathArray[j], pathArray[j + 1]);
	}
	var polygonOptions = {
		strokeColor : "#B40404",
		strokeWeight : 2,
		fillColor : "#000000",
		fillOpacity : 0.2,
		map : this.map,
		paths : mvcArray,
		clickable : true,
		editable : false,
		draggable : false,
		zIndex : 1
	};
	var polygon = new google.maps.Polygon(polygonOptions);
	var n = this.publicBlogs.length;
	this.publicBlogs[n] = new Blog(polygon, blogId, title, description, time,
			'polygon', userId);
	google.maps.event.addListener(polygon, 'click', function(ev) {
		listenerPublish(blogId, title, description, files, time);
		app.currentRoom.infowindow.close();
		app.currentRoom.infowindow.setPosition(ev.latLng);
		app.currentRoom.infowindow.setContent(title);
		app.currentRoom.infowindow.open(app.currentRoom.map);
		app.currentRoom.select(blogId);
	});
};

Room.prototype.addCommentRoom = function(blogId, body, userName, time) {
	for (var i = 0; i < this.publicBlogs.length; i++) {
		if (this.publicBlogs[i].id == blogId) {
			this.publicBlogs[i].addComment(userName, body, time);
			addOneComment(blogId, userName, body, time);
			break;
		}
	}
};

Room.prototype.findUser = function(userName) {
	for (var i = 0; i < app.users.length; i++) {
		if (app.users[i].name == userName)
			return app.users[i];
	}
	return false;
};

Room.prototype.publishMyPosition = function(userId, lat, lng) {
	if (userId != $("#userName").val()) {
		var user = this.findUser(userId);
		if (user == false)
			setOtherPosition(userId, lat, lng);
		else
			changeOtherPosition(user, lat, lng);
	}
};

Room.prototype.addLike = function(blogId) {
	addLikeByBlogId(blogId);
};
Room.prototype.addDislike = function(blogId) {
	addDislikeByBlogId(blogId);
};

Room.prototype.doSearch = function(txt) {
	var text = txt.toLowerCase();
	var matchingBlogs = [];
	var j = 0;
	for (var i = 0; i < this.publicBlogs.length; i++) {
		if (this.publicBlogs[i].id == text
				|| this.publicBlogs[i].title.toLowerCase().indexOf(text) != -1
				|| this.publicBlogs[i].description.toLowerCase().indexOf(text) !== -1) {
			matchingBlogs[j++] = this.publicBlogs[i];
		} else {
			for (var k = 0; k < this.publicBlogs[i].comments.length; k++) {
				if (this.publicBlogs[i].comments[k].body.toLowerCase().indexOf(
						text) != -1) {
					matchingBlogs[j++] = this.publicBlogs[i];
					break;
				}
			}
		}
	}
	createMatchingList(matchingBlogs);
};

Room.prototype.goToBlog = function(blogId) {
	if (this.map) {
		var blog = loadBlog(blogId);
		var latlng = null;
		if (blog.type == 'marker')
			latlng = blog.overlay.getPosition();
		else if (blog.type == 'circle')
			latlng = blog.overlay.getCenter();
		else if (blog.type == 'rectangle')
			latlng = blog.overlay.getBounds().getSouthWest();
		else if (blog.type == 'polygon' || blog.type == 'polyline')
			latlng = blog.overlay.getPath().getAt(0);
		this.map.setCenter(latlng);
		if ($("#panelD").css("display") == 'block')
			$("#buttonD").click();
		this.infowindow.close();
		this.infowindow.setPosition(latlng);
		this.infowindow.setContent(blog.title);
		this.infowindow.open(this.map);
	}
};

Room.prototype.myBlogs = function(userId) {
	var matchingBlogs = [];
	var j = 0;
	for (var i = 0; i < this.publicBlogs.length; i++) {
		if (this.publicBlogs[i].userId == userId) {
			matchingBlogs[j++] = this.publicBlogs[i];
		}
	}
	createMyMatchingList(matchingBlogs);
};

Room.prototype.couple = function() {
	this.adapter.coupleObject("room", this, {
		messageType : "EVENT",
		explicitMapping : [ "publish", "addCommentRoom", "publishCircle",
				"publishRectangle", "publishPolyline", "publishPolygon",
				"publishMyPosition", "addLike", "addDislike" ]
	});
	this.personalAdapter.coupleObject("room", this, {
		messageType : "STATE",
		explicitMapping : [ 'select' ],
		getElementState : function() {
			return {
				selected : this.selected
			};
		},
		setElementState : function(state) {
			this._doSelect(state.selected);
		}

	});
};

Room.prototype._doSelect = function(id) {

	if (this.map) {
		if (this.selected == id)
			return;
		this.selected = id;

		var blog = loadBlog(id);
		var latlng = null;
		if (blog.type == 'marker')
			latlng = blog.overlay.getPosition();
		else if (blog.type == 'circle')
			latlng = blog.overlay.getCenter();
		else if (blog.type == 'rectangle')
			latlng = blog.overlay.getBounds().getSouthWest();
		else if (blog.type == 'polygon' || blog.type == 'polyline')
			latlng = blog.overlay.getPath().getAt(0);
		this.map.setCenter(latlng);
		listenerPublish(id, blog.title, blog.description, blog.media, blog.time);
		app.currentRoom.infowindow.close();
		app.currentRoom.infowindow.setPosition(latlng);
		app.currentRoom.infowindow.setContent( blog.title);
		app.currentRoom.infowindow.open(app.currentRoom.map);
	}

};
Room.prototype.select = function(id) {
	if (this.selected == id)
		return;
	this.selected = id;
};